package com.qf.service;
import com.qf.mapper.UserMapper;
import com.qf.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class UserServiceImpl {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Scheduled(cron = "0/15 * * * * ?")
    public void insertUser(){
        System.out.println("定时任务执行" + new Date());

        for (int i = 0; i < 4; i++) {
            //匿名对象方式开启线程
//            Thread thread =new Thread(new Runnable() {
//                @Override
//                public void run() {
//                   for (int j = 0; j < 20; j++) {
//                    userMapper.insertUser(UserUtils.users(50000));
//                }
//            });

            //lamada方式
            Thread thread =new Thread(() ->{
                for (int j = 0; j < 30; j++) {
                    userMapper.insertUser(UserUtils.users(50000));
                }
            });
           threadPoolExecutor.execute(thread);

        }

    }

}
